Note
Go to the end to download the full example code.
Calculate the ground state of a 2d quantum Ising model.
Simple example on how to set up a Quantum ising 2-dimensional ground state search
# pylint: disable=invalid-name
import numpy as np
import numpy.linalg as nla
import qtealeaves as qtl
from qtealeaves.models import get_quantum_ising_2d
We first define a simple function that returns thr TRUE ground state energy value previously computed. In this way, we can be sure the final result will be correct
def get_osmps_reference():
"""
Return ground state reference value from OSMPS simulation.
"""
ref_osmps = -24.710085956694
return ref_osmps
We prefer to use a main method to avoid the automatic run when imported
def main(tn_type=5, input_folder=None, output_folder=None):
"""
Main method for the ground state simulation of a
quantum Ising model in 2D.
**Arguments**
tn_type : int, optional
Choose 5 for python-TTN, 6 for python-MPS.
Default to 5.
input_folder : str | None, optional
Input folder. Default to None.
output_folder : str | None, optional
Output folder. Default to None.
"""
Defining the model, in this case, is very simple, since it is one of the
already available models of the library. However, defining a model is simple
even starting from scratch! For an example see :docs:`other_example` or the
code of get_quantum_ising_2d()
model, my_ops = get_quantum_ising_2d()
We define parametric I/O folder to keep the results more ordered. As you see, they are parametrized through the size of the chain, i.e. the number of physical sites of the Tensor network
if input_folder is None:
input_folder = lambda params: "QI2d/input_L%d" % (params["L"])
if output_folder is None:
output_folder = lambda params: "QI2d/output_L%d" % (params["L"])
We define the convergence parameters and the observables: they are really important:
The convergence parameters ensure we have a relaiable result. See :docs:`/../chapters/convergence` for further informations about them.
The observables ensure we are measuring (and storing) something at the end of the simulation! See :docs:`/../chapters/measurements` for further informations about the available observables.
my_conv = qtl.convergence_parameters.TNConvergenceParameters(
max_iter=5, max_bond_dimension=16
)
my_obs = qtl.observables.TNObservables()
Define the simulation instance
simulation = qtl.QuantumGreenTeaSimulation(
model,
my_ops,
my_conv,
my_obs,
tn_type=tn_type,
folder_name_input=input_folder,
folder_name_output=output_folder,
store_checkpoints=False,
)
Define the parameters of the models: here we define the ‘L’ seen in the I/O definition at the beginning! Instead ‘J’ and ‘g’ are important model parameters
params = [
{
"L": 4,
# model parameters
"J": 1.0,
"g": 0.5,
}
]
We finally run the simulation, checking that everything is ok
simulation.run(params, delete_existing_folder=True)
for elem in params:
tn_energy_0 = simulation.get_static_obs(elem)["energy"]
if elem["L"] == 2:
ed_energy_0, _ = nla.eigh(model.build_ham(my_ops, elem))[0][:2]
print("ED vs TN ground state energy", ed_energy_0, tn_energy_0)
assert np.abs(tn_energy_0 - ed_energy_0) < 1e-4
msg_check = "ground state energy vs ED at least correct up to 1e-4."
elif elem["L"] == 4:
# Can compare to OSMPS
ref_osmps = get_osmps_reference()
print("QTea vs OSMPS ground state energy", tn_energy_0, ref_osmps)
assert np.abs(tn_energy_0 - ref_osmps) < 1e-4
msg_check = "ground state energy vs OSMPS at least correct up to 1e-4."
else:
print("Ground state energy", tn_energy_0)
msg_check = "No checks implemented for this simulation."
print(f"\nExample `{__file__}` ran successfully; " + msg_check)
return
Run the code
if __name__ == "__main__":
main()